/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package admincommands;

import java.util.Iterator;

import com.aionemu.gameserver.configs.main.CustomConfig;
import com.aionemu.gameserver.configs.administration.AdminConfig;
import com.aionemu.gameserver.model.gameobjects.player.FriendList;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.player.FriendList.Status;
import com.aionemu.gameserver.utils.PacketSendUtility;
import com.aionemu.gameserver.utils.chathandlers.AdminCommand;
import com.aionemu.gameserver.world.World;

/**
 * @author Aion Gates
 *
 */
 public class GMList extends AdminCommand
{

	public GMList()
	{
		super("gmlist");
	}

	/**
	 * {@inheritDoc}
	 */

	@Override
	public void executeCommand(Player admin, String[] params)
	{
		if(admin.getAccessLevel() < AdminConfig.COMMAND_GMLIST)
		{
			PacketSendUtility.sendMessage(admin, "You dont have enough rights to execute this command");
			return;
		}

		int GMCount = 0;
		int ADMINCount = 0;
		int tGMCount = 0;
		String sGMNames = "";
		String sADMINNames = "";
		String stGMNames = "";

		/*
		 * prints header
		 */

		PacketSendUtility.sendMessage(admin, " ---------------------------------");
		PacketSendUtility.sendMessage(admin, "|             GM-List             |");
		PacketSendUtility.sendMessage(admin, " ---------------------------------");

	if (CustomConfig.SHOW_GMLIST_GM)
	{
		/*
		 * looks if a online Player is GM
		 */

		Iterator<Player> pi = World.getInstance().getPlayersIterator();
		while(pi.hasNext())
		{
			Player pPlayer = pi.next();
			if(pPlayer.isGM() && !pPlayer.isProtectionActive() && pPlayer.getFriendList().getStatus() != FriendList.Status.OFFLINE)
			{
				GMCount++;
				sGMNames += pPlayer.getName()+" : "+ returnStringStatus(pPlayer.getFriendList().getStatus()) +";\n";
			}
		}

		if(GMCount == 0)
		{
			PacketSendUtility.sendMessage(admin, "Sorry, no GM online !\n"+" ---------------------------------");
		}
		else if(GMCount == 1)
		{

			PacketSendUtility.sendMessage(admin, "GMs("+String.valueOf( GMCount )+")");
		}
		else
		{
			PacketSendUtility.sendMessage(admin, "GMs("+String.valueOf( GMCount )+")");
		}
		if(GMCount != 0)
			PacketSendUtility.sendMessage(admin, sGMNames+" ---------------------------------");
	}

	if (CustomConfig.SHOW_GMLIST_ADMIN)
	{
		/*
		 * looks if a online Player is ADMIN
		 */

		Iterator<Player> pi2 = World.getInstance().getPlayersIterator();

		while(pi2.hasNext())
		{
			Player pPlayer = pi2.next();
			if(pPlayer.isADMIN() && !pPlayer.isProtectionActive() && pPlayer.getFriendList().getStatus() != FriendList.Status.OFFLINE)
				{
					ADMINCount++;
					sADMINNames += pPlayer.getName()+" : "+ returnStringStatus(pPlayer.getFriendList().getStatus()) +";\n";
				}
		}


		if(ADMINCount == 0)
		{
			PacketSendUtility.sendMessage(admin, "Sorry, no ADMIN online !\n"+" ---------------------------------");
		}
		else if(ADMINCount == 1)
		{
			PacketSendUtility.sendMessage(admin, "ADMINs("+String.valueOf( ADMINCount )+")");
		}
		else
		{
			PacketSendUtility.sendMessage(admin, "ADMINs("+String.valueOf( ADMINCount )+")");
		}
		if(ADMINCount != 0)
			PacketSendUtility.sendMessage(admin, sADMINNames+" ---------------------------------");
	}

	if (CustomConfig.SHOW_GMLIST_tGM)
	{
		/*
		 * looks if a online Player is tGM
		 */

		Iterator<Player> pi1 = World.getInstance().getPlayersIterator();

		while(pi1.hasNext())
		{
			Player pPlayer = pi1.next();
			if(pPlayer.istGM() && !pPlayer.isProtectionActive() && pPlayer.getFriendList().getStatus() != FriendList.Status.OFFLINE)
				{
					tGMCount++;
					stGMNames += pPlayer.getName()+" : "+ returnStringStatus(pPlayer.getFriendList().getStatus()) +";\n";
				}
		}


		if(tGMCount == 0)
		{
			PacketSendUtility.sendMessage(admin, "Sorry, no tGM online !\n"+" ---------------------------------");
		}
		else if(tGMCount == 1)
		{
			PacketSendUtility.sendMessage(admin, "tGMs("+String.valueOf( tGMCount )+")");
		}
		else
		{
			PacketSendUtility.sendMessage(admin, "tGMs("+String.valueOf( tGMCount )+")");
		}
		if(tGMCount != 0)
			PacketSendUtility.sendMessage(admin, stGMNames+" ---------------------------------\n");
	}

	}

	private String returnStringStatus(Status p_status)
	{
		String return_string = "";
		if (p_status == FriendList.Status.ONLINE)
			return_string = "online";
		if (p_status == FriendList.Status.AWAY)
			return_string = "away";
		return return_string;
	}
}



